﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ANZ_AIM_GUI.Utils;
using ANZ_AIM_BLL;
using ANZ_AIM_BLL.Model;
using log4net;


namespace ANZ_AIM_GUI
{
    public partial class frmSaleChangeApproveStatus : Form
    {
        //log4net
        private static readonly ILog logger = LogManager.GetLogger(typeof(frmSaleChangeApproveStatus));

        protected AppParam appParam = AppParam.Instance;
        public string batchFileChangeID { get; set; }

        public frmSaleChangeApproveStatus()
        {
            InitializeComponent();
        }

        public frmSaleChangeApproveStatus(string batchFileChangeID)
            : this()
        {
            this.batchFileChangeID = batchFileChangeID;
        }

        private void btnApprove_Click(object sender, EventArgs e)
        {
            var msg = String.Empty;
            try
            {
                msg = String.Format("{0}, batchFileID: {1}", Constant.SYS_MSG_SALE_APPROVE, batchFileChangeID);

                SaleChangeRequestBLL saleChangeRequestBll = new SaleChangeRequestBLL();
                DataTable dtData = saleChangeRequestBll.GetByBatchFileChangeID(batchFileChangeID);

                List<string> profileIDs = new List<string>();
                List<string> batchfileIDs = new List<string>();

                foreach (DataRow dr in dtData.Rows)
                {
                    if (!dr["OldValue"].Equals(dr["NewValue"]))
                    {
                        if (!string.IsNullOrEmpty(dr[0].ToString()))
                        {
                            profileIDs.Add(dr[0].ToString());
                        }
                        else
                        {
                            batchfileIDs.Add(dr[1].ToString());
                        }
                    }
                }

                profileIDs = profileIDs.Distinct().ToList();
                batchfileIDs = batchfileIDs.Distinct().ToList();

                //Fixing - Dont touch
                Dictionary<string, string> saleInfo = new Dictionary<string, string>();
                saleInfo = new Dictionary<string, string>();
                saleInfo.Add("ID", "ID");// show header when load data from database
                saleInfo.Add("Profile ID", "ProfileID");
                saleInfo.Add("Sale Code", "SaleCode");
                saleInfo.Add("Personal Code", "PersonalCode");

                saleInfo.Add("CV Code", "CVCode"); // 24Jan2015
                saleInfo.Add("Candidate Code", "CVCode"); // 24Jan2015

                saleInfo.Add("Employee Full Name", "EmployeeFullName");
                saleInfo.Add("Employee Full Name_E", "EmployeeFullNameE");
                saleInfo.Add("Role", "Role");
                saleInfo.Add("Level", "Level");
                saleInfo.Add("Category", "Category");
                saleInfo.Add("Product Category", "ProductCategory");
                saleInfo.Add("Product", "Product");
                saleInfo.Add("Team Name", "TeamName");
                saleInfo.Add("Team Leader Code", "TeamLeaderCode");
                saleInfo.Add("Channel", "Channel");
                saleInfo.Add("Joining Date", "JoiningDate");
                saleInfo.Add("Promotion Transfer Date", "PromotionTransferDate");
                saleInfo.Add("Status", "Status");

                saleInfo.Add("ID Card Number", "IDCardNumber");
                saleInfo.Add("Date Of Birth", "DateOfBirth");
                saleInfo.Add("ID Issued Date", "IDIssuedDate");
                saleInfo.Add("ID Issued Place", "IDIssuedPlace");
                saleInfo.Add("Permanent Address", "PermanentAddress");
                saleInfo.Add("Mobile Phone", "MobilePhone");
                saleInfo.Add("Finger ID", "FingerID");
                saleInfo.Add("Personal Tax Code", "PersonalTaxCode");

                saleInfo.Add("Contract Type", "ContractType");
                saleInfo.Add("Contract Term", "ContractTerm");
                saleInfo.Add("Contractor Name", "ContractorName");
                saleInfo.Add("Contract Sign Date", "ContractSignDate");
                saleInfo.Add("Contract Expiry Date", "ContractExpiryDate");

                saleInfo.Add("Bank Account", "BankAccount");
                saleInfo.Add("Bank Branch Address", "BankBranchAddress");
                saleInfo.Add("Bank Name", "BankName");
                saleInfo.Add("Filling Ref Code", "FillingRefCode");
                saleInfo.Add("Profressive Target", "ProfressiveTarget");

                saleInfo.Add("BatchFileID", "BatchFileID");

                foreach (DataRow dr in dtData.Rows)
                {
                    dr["FieldChange"] = saleInfo[dr["FieldChange"].ToString()];
                }

                SaleInfoBLL saleInfoBll = new SaleInfoBLL();
                saleInfoBll.Update(dtData, profileIDs, batchfileIDs);

                //approve batch file
                BatchFileChangeUploadBLL batchFileChangeUploadBll = new BatchFileChangeUploadBLL();
                batchFileChangeUploadBll.UpdateStatus(batchFileChangeID, Constant.APPROVED);

                MessageBox.Show("Approve successfully.", Constant.SYS_INFORMATION, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.InnerException == null ? ex.Message : ex.InnerException.Message, Constant.SYS_ERROR, MessageBoxButtons.OK);
                logger.Error(ex.Message + " - " + ex.StackTrace);
                return;
            }

            try
            {
                if (!String.IsNullOrEmpty(msg))
                {
                    LogBLL logBll = new LogBLL();
                    logBll.Description = msg;
                    logBll.Log(this.FindForm(), appParam.UserName);
                }
            }
            catch { }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnPending_Click(object sender, EventArgs e)
        {
            var msg = String.Empty;
            try
            {
                msg = String.Format("{0}, batchFileID: {1}", Constant.SYS_MSG_SALE_PENDING, batchFileChangeID);

                BatchFileChangeUploadBLL batchFileChangeUploadBll = new BatchFileChangeUploadBLL();
                batchFileChangeUploadBll.UpdateStatus(batchFileChangeID, Constant.PENDING);
                MessageBox.Show("Pending successfully.", Constant.SYS_INFORMATION, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.InnerException == null ? ex.Message : ex.InnerException.Message, Constant.SYS_ERROR, MessageBoxButtons.OK);
                logger.Error(ex.Message + " - " + ex.StackTrace);
                return;
            }

            try
            {
                if (!String.IsNullOrEmpty(msg))
                {
                    LogBLL logBll = new LogBLL();
                    logBll.Description = msg;
                    logBll.Log(this.FindForm(), appParam.UserName);
                }
            }
            catch { }
        }

        private void benReject_Click(object sender, EventArgs e)
        {
            var msg = String.Empty;
            try
            {
                msg = String.Format("{0}, batchFileID: {1}", Constant.SYS_MSG_SALE_REJECT, batchFileChangeID);

                BatchFileChangeUploadBLL batchFileChangeUploadBll = new BatchFileChangeUploadBLL();
                //batchFileChangeUploadBll.Delete(batchFileChangeID); //12Jan2015
                batchFileChangeUploadBll.UpdateStatus(batchFileChangeID, Constant.REJECTED); //12Jan2015

                SaleChangeRequestBLL saleChangeRequetBll = new SaleChangeRequestBLL();
                saleChangeRequetBll.Delete(batchFileChangeID);

                MessageBox.Show("Reject successfully.", Constant.SYS_INFORMATION, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.InnerException == null ? ex.Message : ex.InnerException.Message, Constant.SYS_ERROR, MessageBoxButtons.OK);
                logger.Error(ex.Message + " - " + ex.StackTrace);
                return;
            }

            try
            {
                if (!String.IsNullOrEmpty(msg))
                {
                    LogBLL logBll = new LogBLL();
                    logBll.Description = msg;
                    logBll.Log(this.FindForm(), appParam.UserName);
                }
            }
            catch { }
        }
    }
}
